package letcode.problem.dynamicProgra;


import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.cn/problems/climbing-stairs/description/?envType=study-plan-v2&envId=dynamic-programming
 * */
public class ClimbingStairs {



    /*
    * 记忆化递归
    * */
    public static int recurClimbStairs(int n){
        int memo[] = new int[n + 1];
        return climbStairsMemo(n, memo);
    }

    public static int climbStairsMemo(int n,int memo[]){
        if (memo[n]>0){
            return memo[n];
        }
        if (n == 1) {
            memo[n] = 1;
        }else if (n == 2) {
            memo[n] = 2;
        }else {
            memo[n] = climbStairsMemo(n - 1, memo) + climbStairsMemo(n - 2, memo);
        }
        return memo[n];
    }

    /**
     * 动态规划
     * */
    public static int dpClimbStairs(int n) {
        if (n == 1) {
            return 1;
        }
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }

    /**
     * dp滚动数组
     * */
    public static int dpClimbStairsScrollArray(int n) {
        if (n == 1) {
            return 1;
        }
        int first = 1;
        int second = 2;
        for (int i = 3; i <= n; i++) {
            int third = first + second;
            first = second;
            second = third;
        }
        return second;
    }

    public static void main(String[] args) {
        int i = dpClimbStairs(99);
        System.out.println(i);
    }
}
